package cn.doctorpda.conference.ui.personal;

import android.content.Context;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.adapter.CollectionAdapter;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.collection.CollectionInfoItem;
import cn.doctorpda.conference.bean.collection.DataCollectionInfo;
import cn.doctorpda.conference.presenter.CollectionPresenter;
import cn.doctorpda.conference.ui.BaseFragment;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.ICollectionPageView;
import cn.doctorpda.conference.view.recyclerview.XRecylcerView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/17.
 */
public class CollectionFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, XRecylcerView.LoadingListener, View.OnClickListener, ICollectionPageView {

    private SwipeRefreshLayout checkRefreshLayout;
    private XRecylcerView checkXRecylcerView;
    private CollectionAdapter mAdapter ;
    private CollectionPresenter collectionPresenter;
    private int pageNumber = 1;
    private View view;
    private LinearLayout ll_no_collection;
    private TextView tv_no_data;
    private ImageView imv_no_data;

    public static CollectionFragment newInstance() {
        CollectionFragment fragment = new CollectionFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mAdapter= new CollectionAdapter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {

            return view;
        }

        view = inflater.inflate(R.layout.fragment_my_collection, container, false);
        initView(view);
        initData();
        return view;
    }

    //进入详情页操作后刷新收藏页面
    @Override
    public void onResume() {
        super.onResume();
        collectionPresenter.getCollectionData("1", "10");
    }

    private void initData() {
        collectionPresenter = new CollectionPresenter(this);
        collectionPresenter.getCollectionData("1", "10");
    }

    private void initView(View view) {

        view.findViewById(R.id.btn_back).setOnClickListener(this);

        checkRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.info_refresh);
        checkXRecylcerView = (XRecylcerView) view.findViewById(R.id.info_loadmore);
        checkXRecylcerView.setHasFixedSize(true);
        checkXRecylcerView.setLoadingListener(this);
        checkXRecylcerView.setLoadingMoreEnabled(true);
        checkRefreshLayout.setOnRefreshListener(this);
        checkXRecylcerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        checkXRecylcerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

        mAdapter.setOnItemClickListener(new CollectionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, CollectionInfoItem data) {
                L.d("收藏构造url的ID"+data.getID()+"与类型"+data.getTYPE()+data.getURL());
                if(data.getTYPE().equals("conference")){
                    DetailActivity.startActivity(getActivity(), data.getTITLE(),data.getID(), data.getTYPE(), data.getURL());//去拼接
                }else{
                    DetailActivity.startActivity(getActivity(), data.getTITLE(),data.getCOLLECT_ID(), data.getTYPE(), data.getCOLLECT_ID());//去拼接
                }

            }
        });

        ll_no_collection = (LinearLayout) view.findViewById(R.id.ll_no_data);
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data_show);
        imv_no_data = (ImageView) view.findViewById(R.id.imv_no_data_show);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_back:
                getActivity().finish();
                break;

        }

    }

    @Override
    public String getPageName() {
        return "CollectionFragment";
    }

    @Override
    public void onLoadMore() {
        ++pageNumber;
        collectionPresenter.getCollectionData(pageNumber + "", "10");
    }

    @Override
    public void onRefresh() {
        pageNumber = 1;
        collectionPresenter.getCollectionData("1", "10");
        ll_no_collection.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setPageRefreshResult(Msg msg, DataCollectionInfo data) {

        // 设置当前刷新数，当下拉刷新的时候置为0，上拉加载时已经初始化
        checkXRecylcerView.setPreviousTotal(0);
        // 设置是否还有更多
        checkXRecylcerView.setIsnomore(false);
        checkRefreshLayout.setRefreshing(false);
        mAdapter.clear();
        if (data != null) {
            if (data.getPAGE().getList().isEmpty()) {
                ll_no_collection.setVisibility(View.VISIBLE);
                tv_no_data.setVisibility(View.VISIBLE);
                imv_no_data.setBackgroundResource(R.drawable.nodata);
                tv_no_data.setText("");
            } else {

                ll_no_collection.setVisibility(View.INVISIBLE);
            }
            mAdapter.addAll(data.getPAGE().getList());
        } else {
            ll_no_collection.setVisibility(View.VISIBLE);
            tv_no_data.setVisibility(View.INVISIBLE);
            imv_no_data.setBackgroundResource(R.drawable.shibai);
        }
    }

    @Override
    public void setPageLoadResult(Msg msg, DataCollectionInfo data) {
        if (!msg.isSuccess()) {
            T.showShort(getContext(), msg.getMsg());
        }
        checkXRecylcerView.loadMoreComplete();
        if (data != null) {
            mAdapter.addAll(data.getPAGE().getList());
        }
    }

    @Override
    public void setPageError(Call call, Exception e) {
        mAdapter.clear();
        checkRefreshLayout.setRefreshing(false);
        checkXRecylcerView.loadMoreComplete();
        ll_no_collection.setVisibility(View.VISIBLE);
        tv_no_data.setVisibility(View.INVISIBLE);
        imv_no_data.setBackgroundResource(R.drawable.shibai);
    }
}
